package com.dhcc.bpm.modules.metadata.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.metadata.entity.GateMetadata;
import com.dhcc.bpm.modules.metadata.service.IGateMetadataService;
import com.dhcc.bpm.modules.metadata.vo.GateMetadataVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName GateMetadataController
 * @Description 网关路由元数据关联表 控制器
 * @Author daina
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-metadata/gatemetadata")
@Api(value = "网关路由元数据关联表", tags = "网关路由元数据关联表接口")
public class GateMetadataController extends BladeController {

    private IGateMetadataService gateMetadataService;

    /**
     * @param opcGatewayId
     * @param ids
     * @return org.springblade.core.tool.api.R
     * @Description 批量取消
     * @Author daina
     * @Date 10:42 2020/2/22
     **/
    @PostMapping("/deleteGateMetadata")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    @ApiLog("批量取消渠道信息")
    @Log(title = "", businessType = BusinessType.DELETE)
    public R deleteGateMetadata(@ApiParam(name = "opcGatewayId", value = "网关id") @RequestParam Long opcGatewayId, @ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        int apis = gateMetadataService.deleteGateMetadata(opcGatewayId, ids);
        if (apis > 0) {
            return R.status(true);
        } else {
            return R.status(false);
        }
    }

    /**
     * @param opcGatewayId
     * @param ids
     * @return org.springblade.core.tool.api.R
     * @Description 网关公共参数批量添加元数据
     * @Author daina
     * @Date 10:42 2020/2/22
     **/
    @PostMapping("/addGateMetadata")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "批量添加", notes = "传入opcGatewayId,ids")
    @ApiLog("网关公共参数批量添加元数据")
    @Log(title = "网关公共参数批量添加元数据", businessType = BusinessType.INSERT)
    public R addGateMetadata(@ApiParam(name = "opcGatewayId", value = "网关id") @RequestParam Long opcGatewayId, @ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        List<Long> metaIds = Func.toLongList(ids);
        ArrayList<GateMetadata> list = new ArrayList<>();
        metaIds.forEach(metaId -> {
            GateMetadata gate = new GateMetadata();
            gate.setOpcGatewayId(opcGatewayId);
            gate.setOpcMetadataId(metaId);
            list.add(gate);
        });
        return R.status(gateMetadataService.saveBatch(list));

    }

    /**
     * @param gateMetadata
     * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.metadata.entity.GateMetadata>
     * @Description 详情
     * @Author daina
     * @Date 10:42 2020/2/22
     **/
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入gateMetadata")
    @ApiLog("详情")
    @Log(title = "查询", businessType = BusinessType.SELECT)
    public R<GateMetadata> detail(GateMetadata gateMetadata) {
        GateMetadata detail = gateMetadataService.getOne(Condition.getQueryWrapper(gateMetadata));
        return R.data(detail);
    }

    /**
     * @param gateMetadata
     * @param query
     * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage   <   com.dhcc.bpm.modules.metadata.entity.GateMetadata>>
     * @Description 分页 网关路由元数据关联表
     * @Author daina
     * @Date 10:41 2020/2/22
     **/
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入gateMetadata")
    @ApiLog("分页 网关路由元数据关联表")
    @Log(title = "查询", businessType = BusinessType.SELECT)
    public R<IPage<GateMetadata>> list(GateMetadata gateMetadata, Query query) {
        IPage<GateMetadata> pages = gateMetadataService.page(Condition.getPage(query), Condition.getQueryWrapper(gateMetadata));
        return R.data(pages);
    }

    /**
     * @param gateMetadata
     * @param query
     * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage   <   com.dhcc.bpm.modules.metadata.vo.GateMetadataVO>>
     * @Description 自定义分页 网关路由元数据关联表
     * @Author daina
     * @Date 10:41 2020/2/22
     **/
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入gateMetadata")
    @ApiLog("自定义分页 网关路由元数据关联表")
    @Log(title = "查询", businessType = BusinessType.SELECT)
    public R<IPage<GateMetadataVO>> page(GateMetadataVO gateMetadata, Query query) {
        IPage<GateMetadataVO> pages = gateMetadataService.selectGateMetadataPage(Condition.getPage(query), gateMetadata);
        return R.data(pages);
    }

    /**
     * @param gateMetadata
     * @return org.springblade.core.tool.api.R
     * @Description 新增 网关路由元数据关联表
     * @Author daina
     * @Date 10:41 2020/2/22
     **/
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入gateMetadata")
    @ApiLog("新增 网关路由元数据关联表")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    public R save(@Valid @RequestBody GateMetadata gateMetadata) {
        return R.status(gateMetadataService.save(gateMetadata));
    }

    /**
     * @param gateMetadata
     * @return org.springblade.core.tool.api.R
     * @Description 修改 网关路由元数据关联表
     * @Author daina
     * @Date 10:41 2020/2/22
     **/
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入gateMetadata")
    @ApiLog("修改 网关路由元数据关联表")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    public R update(@Valid @RequestBody GateMetadata gateMetadata) {
        return R.status(gateMetadataService.updateById(gateMetadata));
    }

    /**
     * @param gateMetadata
     * @return org.springblade.core.tool.api.R
     * @Description 新增或修改 网关路由元数据关联表
     * @Author daina
     * @Date 10:41 2020/2/22
     **/
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入gateMetadata")
    @ApiLog("新增或修改 网关路由元数据关联表")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    public R submit(@Valid @RequestBody GateMetadata gateMetadata) {
        return R.status(gateMetadataService.saveOrUpdate(gateMetadata));
    }


    /**
     * @param ids
     * @return org.springblade.core.tool.api.R
     * @Description 删除 网关路由元数据关联表
     * @Author daina
     * @Date 10:41 2020/2/22
     **/
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    @ApiLog("删除 网关路由元数据关联表")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(gateMetadataService.removeByIds(Func.toLongList(ids)));
    }


}
